package com.gxkj.drm.controllers;

import com.alibaba.fastjson.JSONArray;
import com.gxkj.drm.constant.WebConstant;
import com.gxkj.drm.entitys.Cluster;
import com.gxkj.drm.entitys.PushUser;
import com.gxkj.drm.entitys.ResourceManage;
import com.gxkj.drm.enums.RoleType;
import com.gxkj.drm.service.ClusterService;
import com.gxkj.drm.service.ResourceManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by changxiangyang on 2017/9/21.
 */
@Controller
public class ResourceController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceController.class);

    @Resource
    private ClusterService clusterService;

    @Resource
    private ResourceManageService rMService;

    /**
     * 编辑属性
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = WebConstant.PATH_EDIT_RESOURCE)
    public String editResourceByid(Model model, int id) {
        ResourceManage resourceManage = rMService.getResourceManageById(id);
        model.addAttribute("resource", resourceManage);
        return WebConstant.PAGE_EDIT_RESOURCE;
    }

    /**
     * 批量编辑属性（修改集群的单个属性）
     *
     * @param model
     * @param cluster
     * @param attribute
     * @return
     */
    @RequestMapping(value = WebConstant.PATH_BATCH_EDIT_RESOURCE)
    public String batchEditResource(Model model,
                @RequestParam(value = "cluster", required = false, defaultValue = "") String cluster,
                @RequestParam(value = "attribute", required = false, defaultValue = "") String attribute) {
        model.addAttribute("cluster", cluster);
        model.addAttribute("attribute", attribute);
        return WebConstant.PAGE_BATCH_EDIT_RESOURCE;
    }

    /**
     * 移除实例（根据cluster、ip+port移除指定的push服务）
     *
     * @param model
     * @param cluster
     * @param instance
     * @return
     */
    @RequestMapping(value = WebConstant.PATH_RESOURCE_ERMOVE_INSTANCE)
    public String removeInstance(Model model,
                                    @RequestParam(value = "cluster") String cluster,
                                    @RequestParam(value = "instance") String instance) {
        model.addAttribute("cluster", cluster);
        model.addAttribute("instance", instance);
        return WebConstant.PAGE_RESOURCE_ERMOVE_INSTANCE;
    }

    /**
     * 我的资源
     * @param request
     * @param model
     * @param cluster
     * @param attribute
     * @return
     */
    //TODO 待优化
    @RequestMapping(value = WebConstant.PATH_MY_RESOURCE)
    public String resource(HttpServletRequest request, Model model,
               @RequestParam(value = "cluster", required = false) String cluster,
               @RequestParam(value = "instance", required = false) String instance,
               @RequestParam(value = "attribute", required = false) String attribute) {

        List<Cluster> userClusterList = new ArrayList<>();
        // 1.获取用户集群信息
        PushUser user = this.getUser(request);
        int level = user.getLevel();
        if (level == RoleType.USER.getLevel()) {
            String[] clusterArray = StringUtils.split(user.getClusters(), ",");
            // 新注册用户没有集群信息
            if (clusterArray != null) {
                userClusterList = this.clusterService.getClusterInfoByCluster(Arrays.asList(clusterArray));
            }
        } else {
            userClusterList = this.clusterService.getAllCluster();
        }
        JSONArray clusters = new JSONArray();
        clusters.addAll(userClusterList);


        // 2.按条件查找
        List<ResourceManage> resources = new ArrayList<>();
        if (StringUtils.isNotBlank(cluster) && StringUtils.isNotBlank(instance)
                && StringUtils.isNotBlank(attribute)) {
            List<String> selectCluster = new ArrayList<>();
            if ("全部".equals(cluster)) {
                for (Cluster c : userClusterList) {
                    selectCluster.add(c.getName());
                }
            } else {
                selectCluster.add(cluster);
            }
            if ("全部".equals(instance)) {
                instance = "";
            }
            if ("全部".equals(attribute)) {
                attribute = "";
            }
            // 新注册用户没有集群信息
            if (selectCluster.size() > 0) {
                resources = rMService.getResourceManageByParams(selectCluster, instance, attribute);
            }
        }

        model.addAttribute("url", WebConstant.URL_MY_RESOURCE);
        model.addAttribute("clusterSelect", cluster);
        model.addAttribute("instanceSelect", instance);
        model.addAttribute("attributeSelect", attribute);
        model.addAttribute("clusters", clusters.toJSONString());
        model.addAttribute("resources", resources);
        return WebConstant.PAGE_HOME;
    }
}
